{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "разбор ДЗ 6.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.8.12"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T06:27:04.373350Z",
          "start_time": "2019-09-21T06:27:04.188846Z"
        },
        "id": "gpm0UMgkBeJ3"
      },
      "source": [
        "import numpy as np"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ESV6DatYBeLC"
      },
      "source": [
        "#### __1.__ Решить систему уравнений методом Гаусса:\n",
        "\n",
        "### $$\\begin{cases}\n",
        "x_{1}+x_{2}-x_{3}-2x_{4}=0, \\\\\n",
        "2x_{1}+x_{2}-x_{3}+x_{4}=-2, \\\\\n",
        "x_{1}+x_{2}-3x_{3}+x_{4}=4.\\\\\n",
        "\\end{cases}$$"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WRoEZIn2BeLU"
      },
      "source": [
        "Запишем систему в виде расширенной матрицы.\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "\\left.\\begin{matrix}\n",
        "1 & 1 & -1 & -2 \\\\ \n",
        "2 & 1 & -1 & 1 \\\\ \n",
        "1 & 1 & -3 & 1\\\\\n",
        "\\end{matrix}\n",
        "\\right|\\begin{matrix}\n",
        "0\\\\ \n",
        "-2\\\\\n",
        "4\\\\\n",
        "\\end{matrix}\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Умножим 1 строку на -2 и сложим со 2 строкой.\n",
        "Умножим 1 строку на -1 и сложим с 3 строкой.\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "\\left.\\begin{matrix}\n",
        "1 & 1 & -1 & -2 \\\\ \n",
        "0 & -1 & 1 & 5 \\\\ \n",
        "0 & 0 & -2 & 3\\\\\n",
        "\\end{matrix}\n",
        "\\right|\\begin{matrix}\n",
        "0\\\\ \n",
        "-2\\\\\n",
        "4\\\\\n",
        "\\end{matrix}\n",
        "\\end{pmatrix}$$\n",
        "\n",
        "Разделим каждую строку матрицы на соответсвующий ведущий элемент: 1 строку на 1, 2 строку на -2, 3 строку на -2.<br>\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "\\left.\\begin{matrix}\n",
        "1 & 1 & -1 & -2 \\\\ \n",
        "0 & 1 & -1 & -5 \\\\ \n",
        "0 & 0 & 1 & -\\frac{3}{2}\n",
        "\\end{matrix}\\right|\n",
        "\\begin{matrix}\n",
        "0\\\\ \n",
        "2\\\\\n",
        "-2\n",
        "\\end{matrix}\n",
        "\\end{pmatrix}.$$\n",
        "\n",
        "Выразим $x_{1}, x_{2}, x_{3}$ относительно остальных переменных.<br>\n",
        "\n",
        "### $$x_{1} = -x_{2} + x_{3} + 2x_{4}$$\n",
        "### $$x_{2} = 2 + x_{3} + 5x_{4}$$\n",
        "### $$x_{3} = -2 + \\frac{3}{2}x_{4}$$\n",
        "\n",
        "Решение:\n",
        "### $$x_{1} = -2 - 3x_{4}$$\n",
        "### $$x_{2} = \\frac{13}{2}x_{4}$$\n",
        "### $$x_{3} = -2 + \\frac{3}{2}x_{4}$$\n",
        "### $$x_{4} \\mbox{ - произвольное действительное число}$$\n",
        "\n",
        "Частное решение:\n",
        "### $$x_{1} = -2$$\n",
        "### $$x_{2} = 0$$\n",
        "### $$x_{3} = -2$$\n",
        "### $$x_{4} = 0$$"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QLfsDTboBeLf"
      },
      "source": [
        "#### __2.__ Проверить на совместность и выяснить, сколько решений будет иметь система линейных уравнений:\n",
        "\n",
        "   а) $\\begin{cases}\n",
        "3x_{1}-x_{2}+x_{3}=4, \\\\\n",
        "2x_{1}-5x_{2}-3x_{3}=-17, \\\\\n",
        "x_{1}+x_{2}-x_{3}=0;\n",
        "\\end{cases}$\n",
        "    \n",
        "   б) $\\begin{cases}\n",
        "2x_{1}-4x_{2}+6x_{3}=1, \\\\\n",
        "x_{1}-2x_{2}+3x_{3}=-2, \\\\\n",
        "3x_{1}-6x_{2}+9x_{3}=5;\n",
        "\\end{cases}$\n",
        "    \n",
        "   в) $\\begin{cases}\n",
        "x_{1}+2x_{2}+5x_{3}=4, \\\\\n",
        "3x_{1}+x_{2}-8x_{3}=-2. \n",
        "\\end{cases}$"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "BOv3r16oBeLi"
      },
      "source": [
        "__Теорема__ \n",
        "\n",
        "Необходимым и достаточным условием совместности системы из $m$ уравнений с $n$ неизвестными является равенство между собой рангов матрицы коэффициентов $A$ и расширенной матрицы $\\tilde A$\n",
        "\n",
        "$$rank A=rank \\tilde A.$$"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OkXHe2z7BeLs"
      },
      "source": [
        "Причем:<br>\n",
        "1) если $rankA=rank \\tilde A=n$, где $n$ — число неизвестных, то система определена, т. е. имеет единственное решение;<br>\n",
        "2) если $rankA=rank \\tilde A<n$, то система имеет бесконечное количество решений;<br>\n",
        "3) если $rankA<rank \\tilde A$, то система несовместна."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T06:27:04.424215Z",
          "start_time": "2019-09-21T06:27:04.375346Z"
        },
        "id": "NGM81RHwBeLv",
        "outputId": "3be2b682-d481-459e-c828-4cc0ee0f782f"
      },
      "source": [
        "A = np.array([[3, -1, 1], [2, -5, -3], [1, 1, -1]])\n",
        "b = np.array([4, -17, 0])\n",
        "A_ext = np.column_stack([A, b])\n",
        "\n",
        "rank_A = np.linalg.matrix_rank(A)\n",
        "rank_A_ext = np.linalg.matrix_rank(A_ext)\n",
        "n = len(A[0])\n",
        "\n",
        "print(f'A:\\n{A}\\n')\n",
        "print(f'A_ext:\\n{A_ext}\\n')\n",
        "\n",
        "print(f'rank(A): {rank_A}\\n')\n",
        "print(f'rank(A_ext): {rank_A_ext}\\n')\n",
        "print(f'# of vars: {n}\\n')\n",
        "\n",
        "if rank_A == rank_A_ext == n:\n",
        "    print(f'Система имеет единственное решение.')\n",
        "elif rank_A == rank_A_ext < n:\n",
        "    print(f'Система имеет бесконечное кол-во решений.')\n",
        "elif rank_A < rank_A_ext:\n",
        "    print(f'Система несовместна.')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "A:\n",
            "[[ 3 -1  1]\n",
            " [ 2 -5 -3]\n",
            " [ 1  1 -1]]\n",
            "\n",
            "A_ext:\n",
            "[[  3  -1   1   4]\n",
            " [  2  -5  -3 -17]\n",
            " [  1   1  -1   0]]\n",
            "\n",
            "rank(A): 3\n",
            "\n",
            "rank(A_ext): 3\n",
            "\n",
            "# of vars: 3\n",
            "\n",
            "Система имеет единственное решение.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T06:27:04.443166Z",
          "start_time": "2019-09-21T06:27:04.427208Z"
        },
        "id": "bTTgX1jzBeL0",
        "outputId": "b21b7a81-6382-44df-84d0-d01ac6d19fd8"
      },
      "source": [
        "A = np.array([[2, -4, 6], [1, -2, 3], [3, -6, 9]])\n",
        "b = np.array([1, -2, 5])\n",
        "A_ext = np.column_stack([A, b])\n",
        "\n",
        "rank_A = np.linalg.matrix_rank(A)\n",
        "rank_A_ext = np.linalg.matrix_rank(A_ext)\n",
        "n = len(A[0])\n",
        "\n",
        "print(f'A:\\n{A}\\n')\n",
        "print(f'A_ext:\\n{A_ext}\\n')\n",
        "\n",
        "print(f'rank(A): {rank_A}\\n')\n",
        "print(f'rank(A_ext): {rank_A_ext}\\n')\n",
        "print(f'# of vars: {n}\\n')\n",
        "\n",
        "if rank_A == rank_A_ext == n:\n",
        "    print(f'Система имеет единственное решение.')\n",
        "elif rank_A == rank_A_ext < n:\n",
        "    print(f'Система имеет бесконечное кол-во решений.')\n",
        "elif rank_A < rank_A_ext:\n",
        "    print(f'Система несовместна.')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "A:\n",
            "[[ 2 -4  6]\n",
            " [ 1 -2  3]\n",
            " [ 3 -6  9]]\n",
            "\n",
            "A_ext:\n",
            "[[ 2 -4  6  1]\n",
            " [ 1 -2  3 -2]\n",
            " [ 3 -6  9  5]]\n",
            "\n",
            "rank(A): 1\n",
            "\n",
            "rank(A_ext): 2\n",
            "\n",
            "# of vars: 3\n",
            "\n",
            "Система несовместна.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T06:27:04.460119Z",
          "start_time": "2019-09-21T06:27:04.445160Z"
        },
        "id": "elaVORPrBeL2",
        "outputId": "834de3a4-0006-4a87-d669-20dadb27b263"
      },
      "source": [
        "A = np.array([[1, 2, 5], [3, 1, -8]])\n",
        "b = np.array([4, -2])\n",
        "A_ext = np.column_stack([A, b])\n",
        "\n",
        "rank_A = np.linalg.matrix_rank(A)\n",
        "rank_A_ext = np.linalg.matrix_rank(A_ext)\n",
        "n = len(A[0])\n",
        "\n",
        "print(f'A:\\n{A}\\n')\n",
        "print(f'A_ext:\\n{A_ext}\\n')\n",
        "\n",
        "print(f'rank(A): {rank_A}\\n')\n",
        "print(f'rank(A_ext): {rank_A_ext}\\n')\n",
        "print(f'# of vars: {n}\\n')\n",
        "\n",
        "if rank_A == rank_A_ext == n:\n",
        "    print(f'Система имеет единственное решение.')\n",
        "elif rank_A == rank_A_ext < n:\n",
        "    print(f'Система имеет бесконечное кол-во решений.')\n",
        "elif rank_A < rank_A_ext:\n",
        "    print(f'Система несовместна.')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "A:\n",
            "[[ 1  2  5]\n",
            " [ 3  1 -8]]\n",
            "\n",
            "A_ext:\n",
            "[[ 1  2  5  4]\n",
            " [ 3  1 -8 -2]]\n",
            "\n",
            "rank(A): 2\n",
            "\n",
            "rank(A_ext): 2\n",
            "\n",
            "# of vars: 3\n",
            "\n",
            "Система имеет бесконечное кол-во решений.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DUWwfHGmBeL5"
      },
      "source": [
        "#### __3.__ Проверить на совместность и выяснить, сколько решений будет иметь система линейных уравнений, заданная расширенной матрицей\n",
        "\n",
        "$$\\tilde{A}=\\begin{pmatrix}\n",
        "\\left.\\begin{matrix}\n",
        "1 & 3 & -2 & 4\\\\ \n",
        "0 & 5 & 0 & 1\\\\ \n",
        "0 & 0 & 3 & 0\\\\ \n",
        "0 & 0 & 0 & 2\n",
        "\\end{matrix}\\right|\n",
        "\\begin{matrix}\n",
        "3\\\\ \n",
        "2\\\\\n",
        "4\\\\\n",
        "1\n",
        "\\end{matrix}\n",
        "\\end{pmatrix}.$$"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-21T06:27:04.475082Z",
          "start_time": "2019-09-21T06:27:04.462114Z"
        },
        "id": "MWsICrEyBeL7",
        "outputId": "abca38e7-78d5-4afc-cde0-cd89956fd1d8"
      },
      "source": [
        "A = np.array([[1, 3, -2, 4], [0, 5, 0, 1], [0, 0, 3, 0], [0, 0, 0, 2]])\n",
        "b = np.array([3, 2, 4, 1])\n",
        "A_ext = np.column_stack([A, b])\n",
        "\n",
        "rank_A = np.linalg.matrix_rank(A)\n",
        "rank_A_ext = np.linalg.matrix_rank(A_ext)\n",
        "n = len(A[0])\n",
        "\n",
        "print(f'A:\\n{A}\\n')\n",
        "print(f'A_ext:\\n{A_ext}\\n')\n",
        "\n",
        "print(f'rank(A): {rank_A}\\n')\n",
        "print(f'rank(A_ext): {rank_A_ext}\\n')\n",
        "print(f'# of vars: {n}\\n')\n",
        "\n",
        "if rank_A == rank_A_ext == n:\n",
        "    print(f'Система имеет единственное решение.')\n",
        "elif rank_A == rank_A_ext < n:\n",
        "    print(f'Система имеет бесконечное кол-во решений.')\n",
        "elif rank_A < rank_A_ext:\n",
        "    print(f'Система несовместна.')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "A:\n",
            "[[ 1  3 -2  4]\n",
            " [ 0  5  0  1]\n",
            " [ 0  0  3  0]\n",
            " [ 0  0  0  2]]\n",
            "\n",
            "A_ext:\n",
            "[[ 1  3 -2  4  3]\n",
            " [ 0  5  0  1  2]\n",
            " [ 0  0  3  0  4]\n",
            " [ 0  0  0  2  1]]\n",
            "\n",
            "rank(A): 4\n",
            "\n",
            "rank(A_ext): 4\n",
            "\n",
            "# of vars: 4\n",
            "\n",
            "Система имеет единственное решение.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4D207N3OBeMG"
      },
      "source": [
        "#### __4.__ Дана система линейных уравнений, заданная расширенной матрицей\n",
        "\n",
        "### $$\\tilde{A}=\\begin{pmatrix}\n",
        "\\left.\\begin{matrix}\n",
        "1 & 2 & 3\\\\ \n",
        "4 & 5 & 6\\\\ \n",
        "7 & 8 & 9\n",
        "\\end{matrix}\\right|\n",
        "\\begin{matrix}\n",
        "a\\\\ \n",
        "b\\\\\n",
        "c\n",
        "\\end{matrix}\n",
        "\\end{pmatrix}.$$\n",
        "\n",
        "Найти соотношение между параметрами $a$, $b$ и $c$, при которых система является несовместной."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2019-09-20T18:41:24.011420Z",
          "start_time": "2019-09-20T18:41:24.000448Z"
        },
        "id": "jqmsiQnuBeMI"
      },
      "source": [
        "Запишем систему в виде расширенной матрицы.<br>\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "\\left.\\begin{matrix}\n",
        "1 & 2 & 3 \\\\ \n",
        "4 & 5 & 6 \\\\ \n",
        "7 & 8 & 9\n",
        "\\end{matrix}\\right|\n",
        "\\begin{matrix}\n",
        "a\\\\ \n",
        "b\\\\\n",
        "c\n",
        "\\end{matrix}\n",
        "\\end{pmatrix}.$$\n",
        "\n",
        "1 строку умножим на 2 и вычтем и 2 строки.<br>\n",
        "1 строку умножим на 3 и вычтем и 3 строки.<br>\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "\\left.\\begin{matrix}\n",
        "1 & 2 & 3 \\\\ \n",
        "2 & 1 & 0 \\\\ \n",
        "4 & 2 & 0\n",
        "\\end{matrix}\\right|\n",
        "\\begin{matrix}\n",
        "a\\\\ \n",
        "b - 2a\\\\\n",
        "c - 3a\n",
        "\\end{matrix}\n",
        "\\end{pmatrix}.$$\n",
        "\n",
        "2 строку умножим на 2 и вычтем из 3 строки. <br>\n",
        "\n",
        "### $$\\begin{pmatrix}\n",
        "\\left.\\begin{matrix}\n",
        "1 & 2 & 3 \\\\ \n",
        "2 & 1 & 0 \\\\ \n",
        "0 & 0 & 0\n",
        "\\end{matrix}\\right|\n",
        "\\begin{matrix}\n",
        "a\\\\ \n",
        "b - 2a\\\\\n",
        "c + a - 2b\n",
        "\\end{matrix}\n",
        "\\end{pmatrix}.$$\n",
        "\n",
        "Если $c + a - 2b \\neq 0$, то система несовместна.<br>"
      ]
    }
  ]
}